import { ref } from "vue";
import { defineStore } from "pinia";
import type { loginForm } from "@/api/user/type";
import { reqLogin } from "@/api/user/user";
import { GET_TOKEN, SET_TOKEN } from "@/utils/token";

const useUserStore = defineStore("userStore", () => {
  const token = ref(GET_TOKEN() || "");

  async function userLogin(data: loginForm) {
    const resp = await reqLogin(data);
    console.log("resp", resp);
    if (resp.code === 200) {
      token.value = resp.data.token;
      SET_TOKEN(resp.data.token);
      return "ok";
    } else {
      return Promise.reject(new Error(resp.message));
    }
  }
  return { userLogin };
});

export default useUserStore;
